package com.bing.bingPro.controller.net;

import cn.hutool.core.text.CharSequenceUtil;
import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.controller.dto.TNetDto;
import com.bing.bingPro.controller.dto.TOrderDateDTO;
import com.bing.bingPro.controller.dto.zTrendDto;
import com.bing.bingPro.controller.vo.TNetworkTopologyPonAreaDayVo;
import com.bing.bingPro.controller.vo.zRankVo;
import com.bing.bingPro.controller.vo.zTrendVo;
import com.bing.bingPro.service.TNetworkTopologyPonAreaDayService;
import com.bing.bingPro.util.ExcelData;
import com.bing.bingPro.util.TNetWorkExportResult;
import com.bing.bingPro.util.WebExcelUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
public class TNetWorkController {

    @Resource
    private TNetworkTopologyPonAreaDayService tNetworkTopologyPonAreaDayService;

    @Resource
    private TNetWorkExportResult tNetWorkExportResult;


    @PostMapping("/zCity")
    public BaseResult<List<Map<String, Object>>> cpList() {
        return new BaseResult<>(this.tNetworkTopologyPonAreaDayService.getCity());
    }

    @PostMapping("/zDate")
    public BaseResult<List<String>> cpList(@RequestBody TOrderDateDTO tOrderDateDTO) {
        return new BaseResult<>(this.tNetworkTopologyPonAreaDayService.dateList(tOrderDateDTO));
    }


    @PostMapping("/zDetail")
    public BaseResult<PageInfo<TNetworkTopologyPonAreaDayVo>> cpList(@RequestBody TNetDto tOrderDateDTO) {
        final PageInfo<TNetworkTopologyPonAreaDayVo> detail = tNetworkTopologyPonAreaDayService.getDetailList(tOrderDateDTO);
        return new BaseResult<>(detail);
    }

    @PostMapping("/exportDetail")
    public void zDetailExport(HttpServletResponse response, @RequestBody TNetDto tOrderDateDTO) throws IOException {
        long startTime = System.currentTimeMillis();
        log.info("PON口统计-获取明细导出开始:" + startTime);
        List<TNetworkTopologyPonAreaDayVo> detailList = tNetworkTopologyPonAreaDayService.getDetail(tOrderDateDTO);
        long selectTime = System.currentTimeMillis();
        log.info("查询数据:" + (selectTime - startTime) + "ms");
        int batchSize = 10000; // 100W
        List<List<TNetworkTopologyPonAreaDayVo>> splitLists = new ArrayList<>();
        for (int i = 0; i < detailList.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, detailList.size());
            List<TNetworkTopologyPonAreaDayVo> subList = detailList.subList(i, endIndex);
            splitLists.add(subList);
        }

        List<ExcelData> excelDataList = new ArrayList<>();
        // 创建每个 sheet 页
        for (int i = 0; i < splitLists.size(); i++) {
            List<TNetworkTopologyPonAreaDayVo> subList = splitLists.get(i);
            ExcelData excelData = new ExcelData(TNetworkTopologyPonAreaDayVo.class, subList);
            excelData.setSheetName("Sheet" + (i + 1)); // 设置 sheet 页名称
            excelDataList.add(excelData);
        }

        // 写出到浏览器
        WebExcelUtil.download(response, "aaa", excelDataList);
        long endTime = System.currentTimeMillis();
        log.info("导出耗时:" + (endTime - selectTime) + "ms");
    }

    //趋势
    @PostMapping("/zTrend")
    public BaseResult<List<zTrendVo>> zTrend(@RequestBody zTrendDto zTrendDto) {
        return new BaseResult<>(this.tNetworkTopologyPonAreaDayService.zTrend(zTrendDto));
    }

    //排名图
    @PostMapping("/zRank")
    public BaseResult<List<zRankVo>> zRank(@RequestBody zTrendDto zTrendDto) {
        return new BaseResult<>(this.tNetworkTopologyPonAreaDayService.zRank(zTrendDto));
    }


    //导出
    @PostMapping("/zRankExport")
    public void zRankExport(HttpServletResponse response, @RequestBody zTrendDto zTrendDto) throws IOException {
        List<Map<String, Object>> exportList = new ArrayList<>();
        List<zTrendVo> trendResult = tNetworkTopologyPonAreaDayService.zTrend(zTrendDto);
        Map<String, Object> temp1 = new HashMap<>();
        temp1.put("name", "PON口承载用户数超限趋势图" + zTrendDto.getTime() + "_上限" + zTrendDto.getThreshold());
        temp1.put("data", trendResult);
        exportList.add(temp1);
        if (CharSequenceUtil.isEmpty(zTrendDto.getRegion())) {
            List<zRankVo> sortResult = tNetworkTopologyPonAreaDayService.zRank(zTrendDto);
            Map<String, Object> temp2 = new HashMap<>();
            temp2.put("name", "PON口承载用户数超限排名图_" + zTrendDto.getSortDataTime() + "_上限" + zTrendDto.getThreshold());
            temp2.put("data", sortResult);
            exportList.add(temp2);
        }
        this.tNetWorkExportResult.ponExport(response, exportList, zTrendDto.getTime(), "PON口承载用户数超限" + zTrendDto.getSortDataTime() + "统计_" + zTrendDto.getSortDataTime() + "_上限" + zTrendDto.getThreshold());
    }

}
